package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.userInfo.entity.Province;

import java.util.List;

public class InlandAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements SectionIndexer {

    private Context mContext = null;

    private List<Province.ResponseBean> provinceList = null;

    //type
    public static final int TYPE_HEADER = 0x01;
    public static final int TYPE_LETTER = 0x02;
    //interface
    private OnItemClickLintener mOnItemClickLintener = null;

    public InlandAdpter(Context context) {
        this.mContext = context;
    }

    public void setProvinceList(List<Province.ResponseBean> list) {
        this.provinceList = list;
    }

    public List<Province.ResponseBean> getProvinceList() {
        return this.provinceList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        View view = null;
        if (viewType == TYPE_HEADER) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_lnland_header_item_view, parent, false);
            viewHolder = new HeaderViewHolder(view);
        } else if (viewType == TYPE_LETTER) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_lnland_letter_item_view, parent, false);
            viewHolder = new LetterViewHolder(view);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof LetterViewHolder) {
            Province.ResponseBean province = this.provinceList.get(position);
            LetterViewHolder letterViewHolder = (LetterViewHolder) holder;

            int section = getSectionForPosition(position);
            if (position == getPositionForSection(section)) {
                letterViewHolder.txt_letter.setVisibility(View.VISIBLE);
                letterViewHolder.txt_letter.setText(province.getSortLetters());
                letterViewHolder.line.setVisibility(View.GONE);
            } else {
                letterViewHolder.txt_letter.setVisibility(View.GONE);
                letterViewHolder.line.setVisibility(View.VISIBLE);
            }

            letterViewHolder.txt_province.setText(province.getTitle());
            final int finalPosition = position;
            letterViewHolder.txt_province.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickLintener != null) {
                        mOnItemClickLintener.onItemClick(finalPosition);
                    }
                }
            });
        } else if (holder instanceof HeaderViewHolder) {
            HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
            headerViewHolder.edt_search.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (mOnItemClickLintener != null) {
                        mOnItemClickLintener.onEditInput(s.toString().trim());
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (provinceList == null) {
            return 0;
        }
        return provinceList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
//        if (position == 0) {
//            return TYPE_HEADER;
//        } else {
            return TYPE_LETTER;
//        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = 0; i < getItemCount() - 1; i++) {
            String sortStr = provinceList.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == sectionIndex) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return provinceList.get(position).getSortLetters().charAt(0);
    }


    public static class HeaderViewHolder extends RecyclerView.ViewHolder {

        private EditText edt_search;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            edt_search = itemView.findViewById(R.id.edt_search);
        }
    }

    public static class LetterViewHolder extends RecyclerView.ViewHolder {

        private TextView txt_letter, txt_province;
        private View line;

        public LetterViewHolder(View itemView) {
            super(itemView);
            txt_letter = itemView.findViewById(R.id.txt_letter);
            txt_province = itemView.findViewById(R.id.txt_province);
            line = itemView.findViewById(R.id.line);
        }
    }


    public void setOnItemClickLintener(OnItemClickLintener lintener) {
        this.mOnItemClickLintener = lintener;
    }

    //interface
    public interface OnItemClickLintener {

        void onEditInput(String content);

        void onItemClick(int position);
    }

}
